﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using kim.Common;
namespace kim
{
    public partial class AddorEditUsers :kim.UI.ManagePage
    {
        protected string type;
        protected int typeid;
        private int Nid;
        protected void Page_Load(object sender, EventArgs e)
        {
            type = KIMRequest.GetString("type");
            Nid = KIMRequest.GetInt("id", 1);
            if (!IsPostBack)
            {
                GetRequestValues();
            }
        }
       #region 信息基本操作==============================
        public void GetRequestValues()
        {
            
            if (type == KIMEnums.ActionEnum.Delete.ToString())
            {
                bool del = new BLL.eg_Admin().Delete(Nid);
                if (del)
                    Response.Redirect("UsersList.aspx");
            }
           
            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                pwd.Visible = false;
                pwd1.Visible = false;
                GetEditValue();
            }
            if (type != KIMEnums.ActionEnum.Edit.ToString() && type != KIMEnums.ActionEnum.Add.ToString() && type != KIMEnums.ActionEnum.Delete.ToString())
                Response.Redirect("../indexpage.aspx");
           
        }
        private void GetEditValue()
        {
            Model.eg_Admin mo = new BLL.eg_Admin().GetModel(Nid);
            txtName.Value = mo.AdminName;
            txtPwd.Value = kim.Common.DESEncrypt.Decrypt(mo.AdminPwd);
            n.Value = Convert.ToString(mo.loginNum);
            if (mo.State == 1)
                CBs.Checked = true;
            else
                CBs.Checked = false;
        }

        private bool DoAdd()
        {
            Model.eg_Admin mo = new Model.eg_Admin();
            mo.AdminName =txtName.Value.Trim();
            mo.AdminPwd = kim.Common.DESEncrypt.Encrypt(txtPwd.Value.Trim());
            if (CBs.Checked)
                mo.State = 1;
            else
                mo.State = 0;
            BLL.eg_Admin bll = new BLL.eg_Admin();
            return bll.Add(mo);
        }
        private bool DoEdit(int id)
        {
            Model.eg_Admin mo = new Model.eg_Admin();
            mo.AdminName = txtName.Value.Trim();
            mo.InTime = DateTime.Now;
            mo.loginNum =int.Parse( n.Value);
            mo.AdminPwd = kim.Common.DESEncrypt.Encrypt(txtPwd.Value.Trim());
            mo.ID = id;
            if (CBs.Checked)
                mo.State = 1;
            else
                mo.State = 0;
            return new BLL.eg_Admin().Update(mo);
        }
#endregion


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (type == KIMEnums.ActionEnum.Edit.ToString())
            {
                if (DoEdit(Nid))
                    lbmsg.Text = "修改成功";
                else
                    lbmsg.Text = "修改失败";
            }
            if (type == KIMEnums.ActionEnum.Add.ToString())
            {
                if (DoAdd())
                    lbmsg.Text = "添加成功";
                else
                    lbmsg.Text = "添加失败";
            }
        }
    }
}